#include "LKH.h"
#include <stdio.h>

static int TrialsMin, TrialsMax, TrialSum, Successes;
static GainType CostMin, CostMax, CostSum;
static double TimeMin, TimeMax, TimeSum;

void InitializeStatistics()
{
    TrialSum = Successes = 0;
    CostSum = 0;
    TimeSum = 0.0;
    TrialsMin = INT_MAX;
    TrialsMax = 0;
    TimeMin = DBL_MAX;
    TimeMax = 0;
    CostMin = PLUS_INFINITY;
    CostMax = MINUS_INFINITY;
}

void UpdateStatistics(GainType Cost, double Time)
{
    if (Trial < TrialsMin)
        TrialsMin = Trial;
    if (Trial > TrialsMax)
        TrialsMax = Trial;
    TrialSum += Trial;
    if (Cost <= Optimum)
        Successes++;
    if (Cost < CostMin)
        CostMin = Cost;
    if (Cost > CostMax)
        CostMax = Cost;
    CostSum += Cost;
    if (Time < TimeMin)
        TimeMin = Time;
    if (Time > TimeMax)
        TimeMax = Time;
    TimeSum += Time;
}

void PrintStatistics()
{
    int _Runs = Runs, _TrialsMin = TrialsMin;
    double _TimeMin = TimeMin;
    GainType _Optimum = Optimum;

    printff("Successes/Runs = %d/%d \n", Successes, Runs);
	 fprintf(OutputFile,"Successes/Runs = %d/%d \n", Successes, Runs);
    if (_Runs == 0)
        _Runs = 1;
    if (_TrialsMin > TrialsMax)
        _TrialsMin = 0;
    if (_TimeMin > TimeMax)
        _TimeMin = 0;
    if (CostMin <= CostMax && CostMin != PLUS_INFINITY) {
        printff
            ("Cost.min = " GainFormat ", Cost.avg = %0.2f, Cost.max = "
             GainFormat "\n", CostMin, (double) CostSum / _Runs, CostMax);
		fprintf
			(OutputFile,"Cost.min = " GainFormat ", Cost.avg = %0.2f, Cost.max = "
			GainFormat "\n", CostMin, (double) CostSum / _Runs, CostMax);
        if (_Optimum == MINUS_INFINITY)
            _Optimum = BestCost;
        if (_Optimum != 0)
		{
            printff
                ("Gap.min = %0.3f%%, Gap.avg = %0.3f%%, Gap.max = %0.3f%%\n",
                 100.0 * (CostMin - _Optimum) / _Optimum,
                 100.0 * (CostSum / _Runs - _Optimum) / _Optimum,
                 100.0 * (CostMax - _Optimum) / _Optimum);
			fprintf
				(OutputFile,"Gap.min = %0.3f%%, Gap.avg = %0.3f%%, Gap.max = %0.3f%%\n",
				100.0 * (CostMin - _Optimum) / _Optimum,
				100.0 * (CostSum / _Runs - _Optimum) / _Optimum,
				100.0 * (CostMax - _Optimum) / _Optimum);
		}
    }
    printff("Trials.min = %d, Trials.avg = %0.1f, Trials.max = %d\n",
            _TrialsMin, 1.0 * TrialSum / _Runs, TrialsMax);
    printff
        ("Time.min = %0.1f sec., Time.avg = %0.1f sec., Time.max = %0.1f sec.\n",
         fabs(_TimeMin), fabs(TimeSum) / _Runs, fabs(TimeMax));
	fprintf(OutputFile,"Trials.min = %d, Trials.avg = %0.1f, Trials.max = %d\n",
		_TrialsMin, 1.0 * TrialSum / _Runs, TrialsMax);
	fprintf
		(OutputFile,"Time.min = %0.1f sec., Time.avg = %0.1f sec., Time.max = %0.1f sec.\n",
		fabs(_TimeMin), fabs(TimeSum) / _Runs, fabs(TimeMax));
}
